package com.jinxin.platform.cdcardpassmgr.controller;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdcardpassmgr.rpc.CdcardpassmgrRpc;
import com.jinxin.platform.cdcardpassmgr.service.ICardpassmgrRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-12-26
 * @Author: Lingnan
 **/
@Api(tags = "车辆管理相关接口")
@RestController
@RequestMapping("/unAuth")
@Slf4j
public class CardpassmgrUnauthController {
    @Autowired
    private ICardpassmgrRecordService recordService;
    @Autowired
    private CdcardpassmgrRpc cdcardpassmgrRpc;

    @GetMapping("/getCarDictItems")
    public JsonResult getDictItems(){
        return cdcardpassmgrRpc.getDictItems("carType");
    }

    @ApiOperation(value = "今日进出统计")
    @GetMapping("/countOpenRecord")
    public JsonResult countOpenRecord(){
        HashMap param = new HashMap();
        Map<String,Object> resultMap = new HashMap<>();
        int totalCount = recordService.countCurrentDayRecord(param);
        if(totalCount <= 0){
            resultMap.put("totalCount", 0);
            return JsonResult.ok("success",resultMap);
        }
        List<Map<String, Integer>> mapList = recordService.countBlByType();
        Map<String,Integer> totalMap = new HashMap<>();
        totalMap.put("totalCount",totalCount);
        mapList.add(totalMap);
        return JsonResult.ok("success",mapList);
    }

    @ApiOperation(value = "根据条件查询当日通行记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,paramType="query",dataType="Integer"),
            @ApiImplicitParam(name="limit",value="每页条数",required=true,paramType="query",dataType="Integer"),
            @ApiImplicitParam(name="passTime",value="通行时间，格式yyyy-mm-dd HH:mm:ss", paramType="query",dataType="String"),
            @ApiImplicitParam(name="cardNum",value="车牌号", paramType="query",dataType="String"),
            @ApiImplicitParam(name="type",value="车辆类型", paramType="query",dataType="String"),
            @ApiImplicitParam(name="direction",value="进出方向", paramType="query",dataType="String")
    })
    @GetMapping("/listCarRecord")
    public Map<String,Object> listCarRecord(@RequestParam int page, @RequestParam int limit,String passTime,
                                   String cardNum,String type,String carNum,String direction){
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("passTime", passTime);
        param.put("cardNum", cardNum);
        param.put("type", type);
        param.put("carNum", carNum);
        if(!StringUtils.isEmpty(direction)){
            param.put("direction", Integer.parseInt(direction));
        }
        param.put("isCurrentDay", "true");
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count", recordService.listPageCount(param));
        resultMap.put("data", recordService.listPage(param));
        return resultMap;
    }

}
